﻿// ********************************
// 2010.11.11
// 拓扑发现辅助类
// Author:卢融凯
// ********************************
using System;
using System.Collections.Generic;
using System.Linq;
using SManage.Model;
using SManage.LibExtension;

namespace SManage.Infrastructure
{
    public static class TopologyHelper
    {
        public static TopologyInfo GetStationTopologyInfo(Device device) {
            var topolotyInfo = new TopologyInfo {
                Name = device.Mib2_system.sysName,
                Description = device.Mib2_system.sysDescr,
                Type = device.Mib2_system.sysServices,
                InterfaceIPs = GetStationInterfaceIPs(device),
                IpRoutes = GetStationIpRoutes(device),
                IpNetToMedias = GetStationIpNetToMedias(device)
            };
            return topolotyInfo;
        }

        private static IList<InterfaceIp> GetStationInterfaceIPs(Device device) {
            var interfaceIps = from interfaceIp in device.Mib2_ip.Mib2_ip_ipAddrEntry
                               select new InterfaceIp {
                                   IP = interfaceIp.ipAdEntAddr.Trim().ToIPAddress(),
                                   Mask = interfaceIp.ipAdEntNetMask.Trim()
                               };
            return interfaceIps.ToList();
        }

        private static IList<IpRoute> GetStationIpRoutes(Device device) {
            var ipRoutes = from ipRoute in device.Mib2_ip.Mib2_ip_ipRouteEntry
                           where ipRoute.ipRouteType == 4
                           select new IpRoute {
                               Dest = ipRoute.ipRouteDest.Trim().ToIPAddress(),
                               Mask = ipRoute.ipRouteMask,
                               Next = ipRoute.ipRouteNextHop.Trim().ToIPAddress()
                           };
            return ipRoutes.Distinct().ToList();
        }

        private static IList<IpNetToMedia> GetStationIpNetToMedias(Device device) {
            var ipNetToMedias = from ipNetToMedia in device.Mib2_ip.Mib2_ip_ipNetToMediaEntry
                                select new IpNetToMedia {
                                    IP = ipNetToMedia.ipNetToMediaNetAddress.Trim().ToIPAddress(),
                                    MAC = ipNetToMedia.ipNetToMediaPhysAddress
                                };
            return ipNetToMedias.ToList();
        }
    }
}
